package com.feihao.zhou.TCP.demoFileUpLoad;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.UUID;

public class ServerDemo {
    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket = new ServerSocket(8088);
        while (true) {
            Socket socket = serverSocket.accept();

            //创建文件 输出流，将网络中读取的文件写入本地文件中：
            FileOutputStream fos = new FileOutputStream("D:\\IdeaProjects\\Job-code\\day16-code-NET\\upLoadDir\\"+ UUID.randomUUID().toString()
                    +".pdf");
            //读取网络中的文件：
            InputStream is = socket.getInputStream();
            int len;
            byte[] bytes = new byte[1024];
            while ((len = is.read(bytes)) != -1) {
                fos.write(bytes, 0, len);
            }

            //回写信息，提示完成：
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
            bw.write("上传成功！");
            bw.newLine();
            bw.flush();

            fos.close();
            bw.close();
            socket.close();
        }

    }
}
